import { getMyClassList,getSubjectList,getStuList } from '@/api/zhuxue'
export default{
	data(){
		return{
			showRight: false,
			subArr:[{}],
			subIndex:0,
			clsIndex:-1,
			subId:0,
			stuId:'',
			startDate:'',
			endDate:'',
			stuName:'',
			subName:'',
			clsList:[],
			subListQt:[],
			subDataNum:-1,
			stuDataNum:-1,
			currClsName:'',
			handler:'handler'
		}
	},
	methods:{
		//获取我的班级 老师用
		loadMyClsList(){
			let field = { account:uni.getStorageSync('token'),showGradeTag:1 };
			let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
			getMyClassList({content:contentStr}).then(res=>{
				let tmpRes = res.data.datas;
				if(tmpRes.classList.length > 0){
					this.clsList = tmpRes.classList;
					this.clsIndex = 0;
					this.currClsName = this.clsList[0].levelC + this.clsList[0].levelD + '班';
					//获取学生
					this.getStuList(this.clsList[0].classId,'handleClick');
				}
				if(tmpRes.subList.length > 0){
					this.subListQt = tmpRes.subList;
					this.subDataNum = 1;
					this.subId = tmpRes.subList[0].subId;
					this.subName = tmpRes.subList[0].subName;
					this.subIndex = 0;
				}
			}).catch(err=>{
				this.subDataNum = 0;
				console.log(err)
			});
		},
		selectCls(index,item){
			var opt = '',gradeNo = '';
			this.clsIndex = index;
			opt = item.checkStatus == 0 ? 'tea' : 'classLeader';
			gradeNo = item.levelC;
			this.isFinishSta = 0;
			this.currClsName = item.levelC + item.levelD + '班';
			
			//获取学生
			this.getStuList(item.classId,'handleClick');
		},
		//获取学科
		getSubList(opt,gradeNo,handleOpt){
			var stuAccount = this.currRole == 'tea' ? '' : this.stuAccount;
			let field = { stuAccount };
			let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
			getSubjectList({content:contentStr}).then(res=>{
				this.subListQt = res.data.datas.subjectList;
				this.subDataNum = 1;
				if(handleOpt == 'handleClick'){
					this.subIndex = 0;
					this.subId = this.subListQt[0].subjectId;
					this.subName = this.subListQt[0].subjectName;
				}
				this.getStudyLogList();
			}).catch(err=>{
				console.log(err)
				this.subDataNum = 0;
				this.subId = 0;
				this.subName = '';
			});
		},
		//获取学生
		getStuList(clsId,opt){
			var field = { classId:clsId };
			let contentStr = this.crypto.encrypt(JSON.stringify( field )); 
			getStuList({content:contentStr}).then(res=>{
				this.stuDataNum = 1;
				var tmpRes = res.data.datas;
				//console.log(tmpRes);
				if(opt == 'handleClick'){
					this.stuName = tmpRes[0].realName;
					this.stuId = tmpRes[0].userId;
					this.stuAccount = tmpRes[0].account;
				}
				uni.setStorageSync('stuList',tmpRes);
				this.getStudyLogList();
			}).catch(err=>{
				this.stuDataNum = 0;
				this.stuName = '';
				this.stuId = '';
				uni.removeStorageSync('stuList');
			});
		},
		bindDateChange(e){
			var tmpCurrDate = this.startDate;
			this.startDate = e;
			if(this.startDate > this.endDate && this.endDate != ''){
				this.util.showToast('开始日期不能大于结束日期');
				this.startDate = tmpCurrDate;
				return;
			}
		},
		bindDateChangeEnd(e){
			var tmpEndDate = this.endDate;
			this.endDate =  e;
			if(this.startDate > this.endDate){
				this.util.showToast('开始日期不能大于结束日期');
				this.endDate = tmpEndDate;
				return;
			}
		},
		sureFilter(){
			if(this.currRole == 'tea'){
				if(this.clsIndex == -1){
					this.util.showToast('请选择班级');
					return;
				}
				if(this.subId == 0 && this.currPage == 'recordDet'){
					this.util.showToast('请去设置关注学科');
					return;
				}
				if(this.stuName == '' && this.currPage == 'recordDet'){
					this.util.showToast('该班级下暂无学生');
					return;
				}
			}else if(this.currRole == 'jz'){
				if(this.childDataNum == 0){
					this.util.showToast('暂无孩子,请去绑定');
					return;
				}
				
				if(this.subId == 0){
					this.util.showToast('请联系老师设置关注学科');
					return;
				}
			}
			this.showRight = false;
			if(this.handler != 'clear'){// clear 查看具体班级是 filter下的stuName文本框清空
				setTimeout(()=>{
					uni.setNavigationBarTitle({
						title:this.stuName + '学习记录'
					})
				},100)
				this.getStudyLogList();
			}
		},
		selectCurrSub(index,item){
			this.subIndex = index;
			this.subId = item.subjectId;
			this.subName = item.subjectName;
		},
		closeDrawer(){
			this.showRight = false;
		}
	}
}